import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


/**
 * transform cells in characters and write them to a file
 * 
 * @author root
 * 
 */
public class Reader {

	/**
	 * characters used
	 */
	private char[] caratteriAscii = { '.', '~', '!', '/', '\\', 's', '2', '(', '}', 'V', 'D', 'G', 'K', 'W', 'M', '@' };

	public Reader(Main main, int fileCreated, int blocchiX) {
		// scelgo il file di output
		JFileChooser fc = new JFileChooser();
		fc.setSelectedFile(new File(".txt"));
		int returnVal = fc.showSaveDialog(main);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			// This is where a real application would save the file.
			File fileSave = new File(file.getAbsolutePath());

			try {
				fileSave.createNewFile();
				try {
					FileWriter fw = new FileWriter(fileSave);
					BufferedWriter bw = new BufferedWriter(fw);
					PrintWriter outFile = new PrintWriter(bw);

					outFile.print("");

					// leggo tutti i file log
					int write = 0;
					for (int i = 1; i <= fileCreated; i++) {
						File toScan = new File("log" + i + ".txt");
						try {
							Scanner c = new Scanner(toScan);
							c.nextLine();
							while (c.hasNextLine()) {
								if (c.nextLine().contains("Now using")) {
									c.nextLine();
									c.nextLine();
									break;
								}
							}

							// caratteri scritti

							while (c.hasNextLine()) {

								if (write == blocchiX) {
									// vado a capo
									outFile.append("\n");
									write = 0;
								}
								String tmp = c.nextLine();
								if (!tmp.contains("Accuracy"))
									if (!tmp.equals("")) {
										outFile.append(caratteriAscii[rilevamentoChar(tmp)]);
										write++;
									}
							}

							c.close();

						} catch (FileNotFoundException e1) {
							JOptionPane.showMessageDialog(null, "Errore nella scrittura del file");
							e1.printStackTrace();
						}

						Runtime.getRuntime().exec("rm log" + i + ".txt");

					}
					JOptionPane.showMessageDialog(null, "File creato");

					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					outFile.close();
				} catch (IOException e) {
					JOptionPane.showMessageDialog(null, "Errore nella scrittura del file");
					e.printStackTrace();
				}
			} catch (IOException e1) {
				JOptionPane.showMessageDialog(null, "Errore nella creazione del file");
				e1.printStackTrace();
			}

		}

	}

	public int rilevamentoChar(String s) {
		double max = 0;
		int indice = 0;
		double now = 0;
		Scanner sc = new Scanner(s);
		for (int i = 0; i < 17; i++) {
			now = Double.parseDouble(sc.next());
		}
		for (int i = 0; i < 16; i++) {
			now = Double.parseDouble(sc.next());
			if (now >= max) {
				max = now;
				indice = i;
			}
		}
		sc.close();
		return indice;
	}

}
